package com.cz.modules.cw.service.impl;

import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.cw.dao.CwParkingDao;
import com.cz.modules.cw.entity.CwParkingEntity;
import com.cz.modules.cw.service.CwParkingService;

import javax.swing.*;


@Service("cwParkingService")
public class CwParkingServiceImpl extends ServiceImpl<CwParkingDao, CwParkingEntity> implements CwParkingService {
    private static final char[] HEX_CHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CwParkingEntity> page = this.page(
                new Query<CwParkingEntity>().getPage(params),
                new QueryWrapper<CwParkingEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Integer> checkEmptycw() {
        List<String> nyouren = new ArrayList<>();
        List<String> syouren = new ArrayList<>();
        Map<String,String> map1 = new HashMap<>();
        map1.put("32","");
        map1.put("29","");
        map1.put("11","");
        map1.put("12","");
        map1.put("13","");
        map1.put("2E","down_");
        map1.put("2F","down_");
        map1.put("30","down_");
        map1.put("31","down_");
        map1.put("14","down_");
        map1.put("15","down_");
        map1.put("16","down_");
        map1.put("17","down_");
        map1.put("18","down_");
        map1.put("19","down_");
        map1.put("1A","down_");
        map1.put("2D","top_");
        map1.put("2C","top_");
        map1.put("2B","top_");
        map1.put("2A","top_");
        map1.put("28","top_");
        map1.put("27","top_");
        map1.put("26","top_");
        map1.put("25","top_");
        map1.put("24","top_");
        map1.put("23","top_");
        map1.put("22","top_");
        map1.put("01","right_");
        map1.put("02","right_");
        map1.put("03","right_");
        map1.put("04","right_");
        map1.put("10","right_");
        map1.put("0F","right_");
        map1.put("0E","right_");
        map1.put("0D","right_");
        map1.put("0C","right_");
        map1.put("0B","right_");
        map1.put("0A","right_");
        map1.put("05","left_");
        map1.put("06","left_");
        map1.put("07","left_");
        map1.put("08","left_");
        map1.put("09","left_");
        map1.put("1B","left_");
        map1.put("1C","left_");
        map1.put("1D","left_");
        map1.put("1E","left_");
        map1.put("1F","left_");
        map1.put("20","left_");
        map1.put("21","left_");
        List<String> listz = new ArrayList<String>(){
            {
                this.add("29");
                this.add("2A");
                this.add("2B");
                this.add("2C");
                this.add("2D");
                this.add("2E");
                this.add("2F");
                this.add("30");
                this.add("31");
                this.add("32");
                this.add("01");
                this.add("02");
                this.add("03");
                this.add("04");
                this.add("05");
                this.add("06");
                this.add("07");
                this.add("08");
                this.add("09");
                this.add("11");
                this.add("12");
                this.add("13");
                this.add("14");
                this.add("15");
                this.add("16");
                this.add("17");
                this.add("18");
                this.add("19");
                this.add("1A");
                this.add("0A");
                this.add("0B");
                this.add("0C");
                this.add("0D");
                this.add("0E");
                this.add("0F");
                this.add("10");
                this.add("1B");
                this.add("1C");
                this.add("1D");
                this.add("1E");
                this.add("1F");
                this.add("20");
                this.add("21");
                this.add("22");
                this.add("23");
                this.add("24");
                this.add("25");
                this.add("26");
                this.add("27");
                this.add("28");
            }
        };

        //查询北区男厕剩余数
        List<String> nanlist = new ArrayList<String>(){
            {
                this.add("29");
                this.add("2A");
                this.add("2B");
                this.add("2C");
                this.add("2D");
                this.add("2E");
                this.add("2F");
                this.add("30");
                this.add("31");
                this.add("32");
            }
        };
        List<String> nvlist1 = new ArrayList<String>(){
            {
                this.add("01");
                this.add("02");
                this.add("03");
                this.add("04");
                this.add("05");
                this.add("06");
                this.add("07");
                this.add("08");
                this.add("09");
                this.add("11");
                this.add("12");
                this.add("13");
                this.add("14");
                this.add("15");
                this.add("16");
                this.add("17");
                this.add("18");
                this.add("19");
                this.add("1A");
            }
        };
        List<String> nvlist2 = new ArrayList<String>(){
            {
                this.add("0A");
                this.add("0B");
                this.add("0C");
                this.add("0D");
                this.add("0E");
                this.add("0F");
                this.add("10");
                this.add("1B");
                this.add("1C");
                this.add("1D");
                this.add("1E");
                this.add("1F");
                this.add("20");
                this.add("21");
                this.add("22");
                this.add("23");
                this.add("24");
                this.add("25");
                this.add("26");
                this.add("27");
                this.add("28");
            }
        };
        int nanS = checkCW("192.168.3.111",6001,nanlist,syouren);
        int nvS1 = checkCW("192.168.3.112",6002,nvlist1,syouren);
        int nvS2 = checkCW("192.168.3.113",6003,nvlist2,syouren);
        int nvS = nvS1 + nvS2;
        int nanN = checkCW("192.168.3.11",6011,nanlist,nyouren);
        int nvN1 = checkCW("192.168.3.12",6012,nvlist1,nyouren);
        int nvN2 = checkCW("192.168.3.13",6013,nvlist2,nyouren);
        int nvN = nvN1 + nvN2;
        List list = new ArrayList();
        list.add(nanS);
        list.add(nanN);
        list.add(nvS);
        list.add(nvN);
        list.add(nyouren);
        list.add(syouren);
        list.add(map1);
        list.add(listz);
        return list;
    }
    //厕所人流量查询
    @Override
    public List<String> rllgc(int i) {
        List<String> rll = new ArrayList<>();
        DatagramSocket socket = null;
        try{
            socket = new DatagramSocket();
            socket.setSoTimeout(600);
            DatagramPacket sendPacket = null;
            byte bytes1[] = null;
            if(i==0) {//北区男厕
                bytes1 = fromHexString("0104000000067008");//
                sendPacket = new DatagramPacket(bytes1, bytes1.length, InetAddress.getByName("192.168.3.16"), 6016);
            }else if(i==1){//南区男厕
                bytes1 = fromHexString("0104000000067008");//
                sendPacket = new DatagramPacket(bytes1, bytes1.length, InetAddress.getByName("192.168.3.116"), 6006);
            }
            else if(i==2){//北区女厕
                bytes1 = fromHexString("020400000006703B");//
                sendPacket = new DatagramPacket(bytes1, bytes1.length, InetAddress.getByName("192.168.3.16"), 6016);
            }else if(i==3){//南区女厕
                bytes1 = fromHexString("020400000006703B");//
                sendPacket = new DatagramPacket(bytes1, bytes1.length, InetAddress.getByName("192.168.3.116"), 6006);
            }
            socket.send(sendPacket);
            //接收返回信息
            while (true) {
                byte[] bytes = new byte[17];
                DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                socket.receive(dp);
                byte[] data = dp.getData();
                String data1 = toHexString(data);
                rll.add(jiexirll(data1));
                break;
            }
            socket.close();
        }catch (Exception e){
            socket.close();
        }finally {
            socket.close();
        }
        return rll;
    }

    //============查询厕所环境参数
    @Override
    public Map<String, String> checkEnvironmentcw() {
        Map<String,String> map = new HashMap<>();
        //查询厕所人流量---map中 key0,1,2,3 代表 mann,mans,womann,womans
        for(int i=0;i<4;i++){
            List<String> rll = rllgc(i);
            if(rll.size()>0){
                map.put(String.valueOf(i),rll.get(0));
            }
        }
        List<String> mannInfo = infoOfWC(0);
        if(mannInfo.size()>0){
            map.put("manntemp",mannInfo.get(0));
            map.put("mannwet",mannInfo.get(1));
            map.put("mannsmell",mannInfo.get(2));
        }
        List<String> mansInfo = infoOfWC(1);
        if(mansInfo.size()>0){
            map.put("manstemp",mansInfo.get(0));
            map.put("manswet",mansInfo.get(1));
            map.put("manssmell",mansInfo.get(2));
        }
        List<String> womannInfo = infoOfWC(2);
        if(womannInfo.size()>0){
           map.put("womanntemp",womannInfo.get(0));
           map.put("womannwet",womannInfo.get(1));
           map.put("womannsmell",womannInfo.get(2));
        }
        List<String> womansInfo = infoOfWC(3);
        if(womansInfo.size()>0){
            map.put("womanstemp",womansInfo.get(0));
            map.put("womanswet",womansInfo.get(1));
            map.put("womanssmell",womansInfo.get(2));
        }
        return map;
    }
    public List<String> infoOfWC(int i) {
        List<String> info = new ArrayList<>();
        DatagramSocket socket = null;
        try{
            socket = new DatagramSocket();
            socket.setSoTimeout(600);
            DatagramPacket sendPacket = null;
            byte bytes1[] = null;
            if(i==0) {//北区男厕
                bytes1 = fromHexString("01030000000AC5CD");//
                sendPacket = new DatagramPacket(bytes1, bytes1.length, InetAddress.getByName("192.168.3.14"), 6014);
            }else if(i==1){//南区男厕
                bytes1 = fromHexString("01030000000AC5CD");//
                sendPacket = new DatagramPacket(bytes1, bytes1.length, InetAddress.getByName("192.168.3.114"), 6004);
            }
            else if(i==2){//北区女厕
                bytes1 = fromHexString("02030000000AC5FE");//
                sendPacket = new DatagramPacket(bytes1, bytes1.length, InetAddress.getByName("192.168.3.14"), 6014);
            }else if(i==3){//南区女厕
                bytes1 = fromHexString("02030000000AC5FE");//
                sendPacket = new DatagramPacket(bytes1, bytes1.length, InetAddress.getByName("192.168.3.114"), 6004);
            }
            socket.send(sendPacket);
            //接收返回信息
            while (true) {
                byte[] bytes = new byte[25];
                DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                socket.receive(dp);
                byte[] data = dp.getData();
                String data1 = toHexString(data);
                info = jiexiInfo(data1);
                break;
            }
            socket.close();
        }catch (Exception e){
            socket.close();
        }finally {
            socket.close();
        }
        return info;
    }

    //udp连接查是否有人
    public Integer checkCW(String ip,int port,List<String> kw,List<String> youren) {
        DatagramSocket socket = null;
        int count = 0;
        for(String s:kw){
            try {
                //创建一个UDP套接字，与本地任意一个未使用的UDP端口绑定
                socket = new DatagramSocket();
                socket.setSoTimeout(600);
                //组织报文，发送报文
                String msg1 = "fe";
                String msg2 = "010000";
                String msg = msg1 + s + msg2;
                byte bytes1[] = fromHexString(msg);
                DatagramPacket sendPacket = new DatagramPacket(bytes1,bytes1.length, InetAddress.getByName(ip),port);
                //给EchoUDPServer发送数据报
                socket.send(sendPacket);
                //接收返回信息
                while (true){
                    byte[] bytes = new byte[5];
                    DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
                    socket.receive(dp);
                    byte[] data = dp.getData();
                    String  data1 = toHexString(data);
                    if(data1.length()==10&&data1.startsWith("f")){
                        int have = data1.charAt(7);
                        if(have == 48){//asc码===没人
                            //count += 1;
                            //youren.add(s);
                        }else{
                            count += 1;
                            youren.add(s);
                        }
                    }
                    socket.close();
                    break;
                }
            }catch (Exception e){
                socket.close();
                //e.printStackTrace();
            }
        }
        return count;
    }

    //将byte类型数组转化成16进制字符串
    public String toHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        int num;
        for (byte b : bytes) {
            num = b < 0 ? 256 + b : b;
            sb.append(HEX_CHAR[num / 16]).append(HEX_CHAR[num % 16]);
        }
        return sb.toString();
    }
    //将16进制字符串转换为byte[]
    public static byte[] fromHexString(String hexString) {
        if (null == hexString || "".equals(hexString.trim())) {
            return new byte[0];
        }

        byte[] bytes = new byte[hexString.length() / 2];
        // 16进制字符串
        String hex;
        for (int i = 0; i < hexString.length() / 2; i++) {
            // 每次截取2位
            hex = hexString.substring(i * 2, i * 2 + 2);
            // 16进制-->十进制
            bytes[i] = (byte) Integer.parseInt(hex, 16);
        }

        return bytes;
    }
    public String jiexirll(String rll){
        if(rll.length()==34){
            String rlltoday = rll.substring(22,30);
            return String.valueOf(Long.parseLong(rlltoday,  16)/2);
        }
        return null;
    }
    public List<String> jiexiInfo(String info){
        System.out.println(info+"==================================厕所参数");
        DecimalFormat df = new DecimalFormat("0.0");
        DecimalFormat df1 = new DecimalFormat("0.00");
        List<String> envi = new ArrayList<>();
        if(info.length() == 50){
            String temp = info.substring(6,10);
            Long l = Long.parseLong(temp, 16);
            Double b = l/10.0;
            envi.add(df.format(b));
            String wet = info.substring(10,14);
            Long l2 = Long.parseLong(wet, 16);
            Double b2 = l2/10.0;
            envi.add(df.format(b2));
            String smell = info.substring(14,18);
            Long l3 = Long.parseLong(smell, 16);
            Double b3 = l3/100.0;
            envi.add(df1.format(b3));
        }
        return envi;
    }

}
